<?php

abstract class Soul_Template_Html_Form_Input extends Soul_Template_Html_Element {

    private $_value = '';
    private $_readonly = FALSE;
    private $_validateRules = array();

    public function getForm() {
        return $this->findParentByType('Soul_Template_Html_Form');
    }

    public function setIdAndName($value) {
        $this->setId($value)->setName($value);
        return $this;
    }

    public function getName() {
        return $this->getAttribute('name');
    }

    public function setName($value) {
        return $this->setAttribute('name', $value);
    }

    public function getValue() {
        return $this->_value;
    }

    public function setValue($value) {
        $this->_value = $value;
        return $this;
    }

    public function getValidateRules() {
        return $this->_validateRules;
    }
    
    public function getReadonly(){
    	return $this->_readonly;
    }
    
    public function setReadonly($value){
    	$this->_readonly = $value;
    	return $this;
    }
    
    public function removeValidateRule($type){
        if(isset($this->_validateRules[$type])){
            unset($this->_validateRules[$type]);
        }
        return $this;
    }

    public function addValidateRule($type, $argument = NULL, $message = NULL) {
        if(isset($this->_validateRules[$type])){
            return $this;
        }
        $this->_validateRules[$type] = array(
            'message' => $message,
            'arguments' => is_null($argument) ? 'true' : $argument
        );
        return $this;
    }

    public function addMultiRule() {
        $num = func_num_args();
        $arg = func_get_args();
        for ($i = 0; $i < $num; $i++):
            if (is_array($arg[$i])) {
                foreach ($arg[$i] as $index => $value) {
                    $this->addValidateRule($index, $value, NULL, NULL);
                }
            }
            else
                $this->addValidateRule($arg[$i], NULL, NULL);
        endfor;
        return $this;
    }

    public function addCharacterRule($message = NULL) {
        return $this->addValidateRule('character', NULL, $message);
    }

    public function addRequiredRule($exp = 'true', $message = NULL) {
        return $this->addValidateRule('required', $exp, $message);
    }

    public function addEmailRule($message = NULL) {
        return $this->addValidateRule('email', NULL, $message);
    }

    public function addDigitsRule($message = NULL) {
        return $this->addValidateRule('digits', NULL, $message);
    }

    public function addNumberRule($message = NULL) {
        return $this->addValidateRule('number', NULL, $message);
    }

    public function addDateRule($message = NULL) {
        return $this->addValidateRule('date', NULL, $message);
    }

    public function addUrlRule($message = NULL) {
        return $this->addValidateRule('url', NULL, $message);
    }

    public function addMinLengthRule($value, $message = NULL) {
        return $this->addValidateRule('minlength', $value, $message);
    }

    public function addMaxLengthRule($value, $message = NULL) {
        return $this->addValidateRule('maxlength', $value, $message);
    }

    public function addRangeLengthRule($min, $max, $message = NULL) {
        return $this->addValidateRule('length', "[$min,$max]", $message);
    }

    public function addMinRule($value, $message = NULL) {
        return $this->addValidateRule('min', $value, $message);
    }

    public function addMaxRule($value, $message = NULL) {
        return $this->addValidateRule('max', $value, $message);
    }

    public function addRangeRule($min, $max, $message = NULL) {
        return $this->addValidateRule('range', "[$min, $max]", $message);
    }
    
    public function addAcceptRule(array $extensions, $message = NULL){
    	return $this->addValidateRule('accept', "'".implode('|', $extensions)."'", $message);
    }

    public function getAttributeHtml() {
        $result = parent::getAttributeHtml();
        $result .= ' ' . $this->getValidateRuleHtml();
        return $result;
    }

    protected function getValidateRuleHtml() {
        $result = '';
        if (!empty($this->_validateRules)) {
            $rules = '';
            $messages = '';
            $result = 'validate="{';
            foreach ($this->_validateRules as $type => $rule) {
                $rules .= $type . ':' . $rule['arguments'] . ',';
                if (isset($rule['message'])) {
                    $messages .= $type . ":'" . $rule['message'] . "',";
                }
            }
            $result .= substr($rules, 0, -1);
            if (!empty($messages)) {
                $result .= ',messages:{' . $messages . '}';
            }
            $result .= '}"';
        }
        return $result;
    }
    
    protected  function render() {
    	if($this->getReadonly()){
    		return $this->renderReadonly();
    	}
    	return $this->renderEditable();

    }

    abstract protected function renderReadonly();

    abstract protected function renderEditable();
}